package com.lz.ccjdemo.act;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lz.ccjdemo.R;
import com.lz.ccjdemo.utils.listener.PerfectClickListener;

import java.util.ArrayList;
import java.util.List;

public class ExpandableListViewActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expandable_listview);
        ExpandableListView expListView = findViewById(R.id.exp_list_view);
        List<GroupData> grouList = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            GroupData groupData = new GroupData();
            groupData.title = "title" + i;

            ItemData itemData = new ItemData();
            itemData.name = "name" + i;
            itemData.count = "count" + i;
            itemData.childList=new ArrayList<>();
            groupData.itemData = itemData;
            grouList.add(groupData);
        }
        ExpAdapter adapter = new ExpAdapter(this, grouList);
        expListView.setAdapter(adapter);
    }

    public class GroupData {
        public String title;
        public ItemData itemData;
    }

    public class ItemData {
        public String name;
        public String count;
        public List<ItemData> childList;
    }


    public class ExpAdapter extends BaseExpandableListAdapter {

        private List<GroupData> groupList;
        private Context context;
        private LayoutInflater inflate;

        public ExpAdapter(Context context, List<GroupData> list) {
            groupList = list;
            this.context = context;
            inflate = LayoutInflater.from(context);
        }

        //父项的个数
        @Override
        public int getGroupCount() {
            return groupList.size();
        }

        //某个父项的子项的个数
        @Override
        public int getChildrenCount(int groupPosition) {
            return 1;
        }

        //获得某个父项
        @Override
        public GroupData getGroup(int groupPosition) {
            return groupList.get(groupPosition);
        }

        //获得某个子项
        @Override
        public ItemData getChild(int groupPosition, int childPosition) {
            return getGroup(groupPosition).itemData;
        }

        //父项的Id
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        //子项的id
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        public class GroupHolder {
            public View itemView;
            public TextView tvTitle;

            public GroupHolder(View view) {
                itemView = view;
                tvTitle = view.findViewById(R.id.tv_title);
            }

        }

        public class ChildHolder {
            public View itemView;
            public LinearLayout layoutForms;
            public TextView btnAdd;

            public ChildHolder(View view) {
                itemView = view;
                layoutForms = view.findViewById(R.id.layout_forms);
                btnAdd = view.findViewById(R.id.btn_add_ck);
            }
        }

        //获取父项的view
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            GroupHolder groupHolder;
            if (convertView == null) {
                convertView = inflate.inflate(R.layout.item_group, parent, false);
                groupHolder = new GroupHolder(convertView);
                convertView.setTag(groupHolder);
            } else {
                groupHolder = (GroupHolder) convertView.getTag();
            }
            GroupData group = groupList.get(groupPosition);
            groupHolder.tvTitle.setText(group.title);
            return convertView;
        }

        //获取子项的view
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            ChildHolder childHolder;
            if (convertView == null) {
                convertView = inflate.inflate(R.layout.item_child, parent, false);
                childHolder = new ChildHolder(convertView);
                convertView.setTag(childHolder);
            } else {
                childHolder = (ChildHolder) convertView.getTag();
            }
            childHolder.btnAdd.setOnClickListener(new PerfectClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    addChildView(childHolder.layoutForms,null);
                }
            });
            ItemData childData = getGroup(groupPosition).itemData;
            List<ItemData> childList = childData.childList;
            if (childList != null && childList.size() > 0) {
                //有数据 需要恢复
                for (ItemData item : childList
                ) {
                    addChildView(childHolder.layoutForms, item);
                }
            }
            return convertView;

        }

        //子项是否可选中,如果要设置子项的点击事件,需要返回true
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        public void addChildView(LinearLayout layoutForms, ItemData data) {
            View itemForms = LayoutInflater.from(context).inflate(R.layout.item_forms, null);
            TextView btnDelete = itemForms.findViewById(R.id.btn_delete);
            TextView btnAdd = itemForms.findViewById(R.id.btn_add);
            TextView tvNameTag = itemForms.findViewById(R.id.tv_name_tag);
            EditText etName = itemForms.findViewById(R.id.et_name);
            EditText etWeight = itemForms.findViewById(R.id.et_weight);
            LinearLayout layout1 = itemForms.findViewById(R.id.layout_forms2);
            if (data != null) {
                tvNameTag.setText(data.name);
                etName.setText(data.name);
                etWeight.setText(data.count);
                if (data.childList != null && data.childList.size() > 0) {
                    for (ItemData item : data.childList
                    ) {
                        addForms2(layout1, item);
                    }
                }
            }
            btnAdd.setOnClickListener(new PerfectClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    addForms2(layout1, null);
                }
            });
            btnDelete.setOnClickListener(new PerfectClickListener() { //防抖操作
                @Override
                protected void onNoDoubleClick(View v) {
                    layoutForms.removeView(itemForms);
                }
            });
            layoutForms.addView(itemForms);
        }

        private void addForms2(LinearLayout layoutView, ItemData data) {
            View itemForms2 = LayoutInflater.from(context).inflate(R.layout.item_forms_datlis, null);
            TextView btnDelete = itemForms2.findViewById(R.id.btn_delete);
            EditText etName = itemForms2.findViewById(R.id.et_name);
            EditText etWeight = itemForms2.findViewById(R.id.et_weight);
            if (data != null) {
                etName.setText(data.name);
                etWeight.setText(data.count);
            }
            layoutView.addView(itemForms2);
            btnDelete.setOnClickListener(new PerfectClickListener() { //防抖操作
                @Override
                protected void onNoDoubleClick(View v) {
                    layoutView.removeView(itemForms2);
                }
            });
        }
    }
}
